﻿#-------------------------------------------------------------------------------
# Name:        OpenLibDll
# Purpose:     Load WingRing0.sys from WinRing0.dll
#
# Author:      SUPER_紫电  QQ:911344755
#
# Created:     15/08/2022
# Copyright:   (c) SUPER_紫电 2022 All rights reserved.
# Licence:     <Modified BSD License>
#-------------------------------------------------------------------------------

PHYSICAL_MEMORY_SUPPORT = False

import os
from ctypes import POINTER, WINFUNCTYPE, c_ubyte
from ctypes.wintypes import BOOL, WORD, DWORD, PBYTE, PWORD, PDWORD
from win32api import LoadLibrary, FreeLibrary, GetProcAddress

BYTE = c_ubyte

DWORD_PTR = POINTER(DWORD)

# DLL
_GetDllStatus = WINFUNCTYPE(None, DWORD)
_GetDllVersion = WINFUNCTYPE(DWORD, PBYTE, PBYTE, PBYTE, PBYTE)
_GetDriverVersion = WINFUNCTYPE(DWORD, PBYTE, PBYTE, PBYTE, PBYTE)
_GetDriverType = WINFUNCTYPE(DWORD)

_InitializeOls = WINFUNCTYPE(BOOL)
_DeinitializeOls = WINFUNCTYPE(None)

# CPU
_IsCpuid = WINFUNCTYPE(BOOL)
_IsMsr = WINFUNCTYPE(BOOL)
_IsTsc = WINFUNCTYPE(BOOL)

_Hlt = WINFUNCTYPE(BOOL)

_Rdmsr = WINFUNCTYPE(DWORD, DWORD , PDWORD , PDWORD)
_Wrmsr = WINFUNCTYPE(DWORD, DWORD , DWORD , DWORD)
_Rdpmc = WINFUNCTYPE(DWORD, DWORD , PDWORD , PDWORD)
_Cpuid = WINFUNCTYPE(DWORD, DWORD , PDWORD , PDWORD, PDWORD, PDWORD)
_Rdtsc = WINFUNCTYPE(DWORD, PDWORD , PDWORD )

_HltTx = WINFUNCTYPE(BOOL, DWORD_PTR)
_RdmsrTx = WINFUNCTYPE(DWORD, DWORD, PDWORD, PDWORD, DWORD_PTR)
_WrmsrTx = WINFUNCTYPE(DWORD, DWORD, DWORD, DWORD, DWORD_PTR)
_RdpmcTx = WINFUNCTYPE(DWORD, DWORD, PDWORD, PDWORD, DWORD_PTR)
_CpuidTx = WINFUNCTYPE(DWORD, DWORD, PDWORD, PDWORD, PDWORD, PDWORD, DWORD_PTR)
_RdtscTx = WINFUNCTYPE(DWORD, PDWORD, PDWORD, DWORD_PTR)

_HltPx = WINFUNCTYPE(BOOL, DWORD_PTR)
_RdmsrPx = WINFUNCTYPE(DWORD, DWORD, PDWORD, PDWORD, DWORD_PTR)
_WrmsrPx = WINFUNCTYPE(DWORD, DWORD, DWORD, DWORD, DWORD_PTR)
_RdpmcPx = WINFUNCTYPE(DWORD, DWORD, PDWORD, PDWORD, DWORD_PTR)
_CpuidPx = WINFUNCTYPE(DWORD, DWORD, PDWORD, PDWORD, PDWORD, PDWORD, DWORD_PTR)
_RdtscPx = WINFUNCTYPE(DWORD, PDWORD, PDWORD, DWORD_PTR)

# I/O
_ReadIoPortByte = WINFUNCTYPE(BYTE, WORD)
_ReadIoPortWord = WINFUNCTYPE(WORD, WORD)
_ReadIoPortDword = WINFUNCTYPE(DWORD, WORD)

_ReadIoPortByteEx = WINFUNCTYPE(BOOL, WORD, PBYTE)
_ReadIoPortWordEx = WINFUNCTYPE(BOOL, WORD, PWORD)
_ReadIoPortDwordEx = WINFUNCTYPE(BOOL, WORD, PDWORD)

_WriteIoPortByte = WINFUNCTYPE(None, WORD, BYTE)
_WriteIoPortWord = WINFUNCTYPE(None, WORD, WORD)
_WriteIoPortDword = WINFUNCTYPE(None, WORD, DWORD)

_WriteIoPortByteEx = WINFUNCTYPE(BOOL, WORD, BYTE)
_WriteIoPortWordEx = WINFUNCTYPE(BOOL, WORD, WORD)
_WriteIoPortDwordEx = WINFUNCTYPE(BOOL, WORD, DWORD)

# PCI
_SetPciMaxBusIndex = WINFUNCTYPE(None, BYTE)

_ReadPciConfigByte = WINFUNCTYPE(BYTE, DWORD, BYTE)
_ReadPciConfigWord = WINFUNCTYPE(WORD, DWORD, BYTE)
_ReadPciConfigDword = WINFUNCTYPE(DWORD, DWORD, BYTE)

_ReadPciConfigByteEx = WINFUNCTYPE(BOOL, DWORD, DWORD, PBYTE)
_ReadPciConfigWordEx = WINFUNCTYPE(BOOL, DWORD, DWORD, PWORD)
_ReadPciConfigDwordEx = WINFUNCTYPE(BOOL, DWORD, DWORD, PDWORD)

_WritePciConfigByte = WINFUNCTYPE(None, DWORD, BYTE, BYTE)
_WritePciConfigWord = WINFUNCTYPE(None, DWORD, BYTE, WORD)
_WritePciConfigDword = WINFUNCTYPE(None, DWORD, BYTE, DWORD)

_WritePciConfigByteEx = WINFUNCTYPE(BOOL, DWORD, DWORD, BYTE)
_WritePciConfigWordEx = WINFUNCTYPE(BOOL, DWORD, DWORD, WORD)
_WritePciConfigDwordEx = WINFUNCTYPE(BOOL, DWORD, DWORD, DWORD)

_FindPciDeviceById = WINFUNCTYPE(DWORD, WORD, WORD, BYTE)
_FindPciDeviceByClass = WINFUNCTYPE(DWORD, BYTE, BYTE, BYTE, BYTE)

# Memory
if PHYSICAL_MEMORY_SUPPORT:
    _ReadDmiMemory = WINFUNCTYPE(DWORD, PBYTE, DWORD, DWORD)
    _ReadPhysicalMemory = WINFUNCTYPE(DWORD, DWORD_PTR, PBYTE, DWORD, DWORD)
    _WritePhysicalMemory = WINFUNCTYPE(DWORD, DWORD_PTR, PBYTE, DWORD, DWORD)

# DLL
GetDllStatus: _GetDllStatus = None
GetDllVersion: _GetDllVersion = None
GetDriverVersion: _GetDriverVersion = None
GetDriverType: _GetDriverType = None

InitializeOls: _InitializeOls = None
DeinitializeOls: _DeinitializeOls = None

# CPU
IsCpuid: _IsCpuid = None
IsMsr: _IsMsr = None
IsTsc: _IsTsc = None

Hlt: _Hlt = None
Rdmsr: _Rdmsr = None
Wrmsr: _Wrmsr = None
Rdpmc: _Rdpmc = None
Cpuid: _Cpuid = None
Rdtsc: _Rdtsc = None

HltTx: _HltTx = None
RdmsrTx: _RdmsrTx = None
WrmsrTx: _WrmsrTx = None
RdpmcTx: _RdpmcTx = None
CpuidTx: _CpuidTx = None
RdtscTx: _RdtscTx = None

HltPx: _HltPx = None
RdmsrPx: _RdmsrPx = None
WrmsrPx: _WrmsrPx = None
RdpmcPx: _RdpmcPx = None
CpuidPx: _CpuidPx = None
RdtscPx: _RdtscPx = None

# I/O
ReadIoPortByte: _ReadIoPortByte = None
ReadIoPortWord: _ReadIoPortWord = None
ReadIoPortDword: _ReadIoPortDword = None

ReadIoPortByteEx: _ReadIoPortByteEx = None
ReadIoPortWordEx: _ReadIoPortWordEx = None
ReadIoPortDwordEx: _ReadIoPortDwordEx = None

WriteIoPortByte: _WriteIoPortByte = None
WriteIoPortWord: _WriteIoPortWord = None
WriteIoPortDword: _WriteIoPortDword = None

WriteIoPortByteEx: _WriteIoPortByteEx = None
WriteIoPortWordEx: _WriteIoPortWordEx = None
WriteIoPortDwordEx: _WriteIoPortDwordEx = None

# PCI
SetPciMaxBusIndex: _SetPciMaxBusIndex = None

ReadPciConfigByte: _ReadPciConfigByte = None
ReadPciConfigWord: _ReadPciConfigWord = None
ReadPciConfigDword: _ReadPciConfigDword = None

ReadPciConfigByteEx: _ReadPciConfigByteEx = None
ReadPciConfigWordEx: _ReadPciConfigWordEx = None
ReadPciConfigDwordEx: _ReadPciConfigDwordEx = None

WritePciConfigByte: _WritePciConfigByte = None
WritePciConfigWord: _WritePciConfigWord = None
WritePciConfigDword: _WritePciConfigDword = None

WritePciConfigByteEx: _WritePciConfigByteEx = None
WritePciConfigWordEx: _WritePciConfigWordEx = None
WritePciConfigDwordEx: _WritePciConfigDwordEx = None

FindPciDeviceById: _FindPciDeviceById = None
FindPciDeviceByClass: _FindPciDeviceByClass = None

if PHYSICAL_MEMORY_SUPPORT:
    ReadDmiMemory: _ReadDmiMemory = None
    ReadPhysicalMemory: _ReadPhysicalMemory = None
    WritePhysicalMemory: _WritePhysicalMemory = None

def is_64bit_os():
    if "PROCESSOR_ARCHITEW6432" in os.environ:
        if os.environ["PROCESSOR_ARCHITEW6432"] == "AMD64":
               return True
    if "64" in os.environ["PROCESSOR_ARCHITECTURE"]:
        return True
    return False

def InitOpenLibSys(hModule):
    # DLL
    global GetDllStatus
    global GetDllVersion
    global GetDriverVersion
    global GetDriverType

    global InitializeOls
    global DeinitializeOls

    # CPU
    global IsCpuid
    global IsMsr
    global IsTsc

    global Hlt
    global Rdmsr
    global Wrmsr
    global Rdpmc
    global Cpuid
    global Rdtsc

    global HltTx
    global RdmsrTx
    global WrmsrTx
    global RdpmcTx
    global CpuidTx
    global RdtscTx

    global HltPx
    global RdmsrPx
    global WrmsrPx
    global RdpmcPx
    global CpuidPx
    global RdtscPx

    # I/O
    global ReadIoPortByte
    global ReadIoPortWord
    global ReadIoPortDword

    global ReadIoPortByteEx
    global ReadIoPortWordEx
    global ReadIoPortDwordEx

    global WriteIoPortByte
    global WriteIoPortWord
    global WriteIoPortDword

    global WriteIoPortByteEx
    global WriteIoPortWordEx
    global WriteIoPortDwordEx

    # PCI
    global SetPciMaxBusIndex

    global ReadPciConfigByte
    global ReadPciConfigWord
    global ReadPciConfigDword

    global ReadPciConfigByteEx
    global ReadPciConfigWordEx
    global ReadPciConfigDwordEx

    global WritePciConfigByte
    global WritePciConfigWord
    global WritePciConfigDword

    global WritePciConfigByteEx
    global WritePciConfigWordEx
    global WritePciConfigDwordEx

    global FindPciDeviceById
    global FindPciDeviceByClass

    if PHYSICAL_MEMORY_SUPPORT:
        global ReadDmiMemory
        global ReadPhysicalMemory
        global WritePhysicalMemory

    if is_64bit_os:
        hModule = LoadLibrary("WinRing0x64.dll")
    else:
        hModule = LoadLibrary("WinRing0x32.dll")
    if hModule == None:
        return False
    #-----------------------------------------------------------------------------
    # GetProcAddress
    #-----------------------------------------------------------------------------
    # DLL
    GetDllStatus =          (_GetDllStatus)          (GetProcAddress(hModule, "GetDllStatus"))
    GetDllVersion =         (_GetDllVersion)         (GetProcAddress(hModule, "GetDllVersion"))
    GetDriverVersion =      (_GetDriverVersion)      (GetProcAddress(hModule, "GetDriverVersion"))
    GetDriverType =         (_GetDriverType)         (GetProcAddress(hModule, "GetDriverType"))

    InitializeOls =         (_InitializeOls)         (GetProcAddress(hModule, "InitializeOls"))
    DeinitializeOls =       (_DeinitializeOls)       (GetProcAddress(hModule, "DeinitializeOls"))

    # CPU
    IsCpuid =               (_IsCpuid)               (GetProcAddress(hModule, "IsCpuid"))
    IsMsr =                 (_IsMsr)                 (GetProcAddress(hModule, "IsMsr"))
    IsTsc =                 (_IsTsc)                 (GetProcAddress(hModule, "IsTsc"))
    Hlt =                   (_Hlt)                   (GetProcAddress(hModule, "Hlt"))
    Rdmsr =                 (_Rdmsr)                 (GetProcAddress(hModule, "Rdmsr"))
    Wrmsr =                 (_Wrmsr)                 (GetProcAddress(hModule, "Wrmsr"))
    Rdpmc =                 (_Rdpmc)                 (GetProcAddress(hModule, "Rdpmc"))
    Cpuid =                 (_Cpuid)                 (GetProcAddress(hModule, "Cpuid"))
    Rdtsc =                 (_Rdtsc)                 (GetProcAddress(hModule, "Rdtsc"))
    HltTx =                 (_HltTx)                 (GetProcAddress(hModule, "HltTx"))
    RdmsrTx =               (_RdmsrTx)               (GetProcAddress(hModule, "RdmsrTx"))
    WrmsrTx =               (_WrmsrTx)               (GetProcAddress(hModule, "WrmsrTx"))
    RdpmcTx =               (_RdpmcTx)               (GetProcAddress(hModule, "RdpmcTx"))
    CpuidTx =               (_CpuidTx)               (GetProcAddress(hModule, "CpuidTx"))
    RdtscTx =               (_RdtscTx)               (GetProcAddress(hModule, "RdtscTx"))
    HltPx =                 (_HltPx)                 (GetProcAddress(hModule, "HltPx"))
    RdmsrPx =               (_RdmsrPx)               (GetProcAddress(hModule, "RdmsrPx"))
    WrmsrPx =               (_WrmsrPx)               (GetProcAddress(hModule, "WrmsrPx"))
    RdpmcPx =               (_RdpmcPx)               (GetProcAddress(hModule, "RdpmcPx"))
    CpuidPx =               (_CpuidPx)               (GetProcAddress(hModule, "CpuidPx"))
    RdtscPx =               (_RdtscPx)               (GetProcAddress(hModule, "RdtscPx"))

    # I/O
    ReadIoPortByte =        (_ReadIoPortByte)        (GetProcAddress(hModule, "ReadIoPortByte"))
    ReadIoPortWord =        (_ReadIoPortWord)        (GetProcAddress(hModule, "ReadIoPortWord"))
    ReadIoPortDword =       (_ReadIoPortDword)       (GetProcAddress(hModule, "ReadIoPortDword"))

    ReadIoPortByteEx =      (_ReadIoPortByteEx)      (GetProcAddress(hModule, "ReadIoPortByteEx"))
    ReadIoPortWordEx =      (_ReadIoPortWordEx)      (GetProcAddress(hModule, "ReadIoPortWordEx"))
    ReadIoPortDwordEx =     (_ReadIoPortDwordEx)     (GetProcAddress(hModule, "ReadIoPortDwordEx"))

    WriteIoPortByte =       (_WriteIoPortByte)       (GetProcAddress(hModule, "WriteIoPortByte"))
    WriteIoPortWord =       (_WriteIoPortWord)       (GetProcAddress(hModule, "WriteIoPortWord"))
    WriteIoPortDword =      (_WriteIoPortDword)      (GetProcAddress(hModule, "WriteIoPortDword"))

    WriteIoPortByteEx =     (_WriteIoPortByteEx)     (GetProcAddress(hModule, "WriteIoPortByteEx"))
    WriteIoPortWordEx =     (_WriteIoPortWordEx)     (GetProcAddress(hModule, "WriteIoPortWordEx"))
    WriteIoPortDwordEx =    (_WriteIoPortDwordEx)    (GetProcAddress(hModule, "WriteIoPortDwordEx"))

    # PCI
    SetPciMaxBusIndex =     (_SetPciMaxBusIndex)     (GetProcAddress(hModule, "SetPciMaxBusIndex"))

    ReadPciConfigByte =     (_ReadPciConfigByte)     (GetProcAddress(hModule, "ReadPciConfigByte"))
    ReadPciConfigWord =     (_ReadPciConfigWord)     (GetProcAddress(hModule, "ReadPciConfigWord"))
    ReadPciConfigDword =    (_ReadPciConfigDword)    (GetProcAddress(hModule, "ReadPciConfigDword"))

    ReadPciConfigByteEx =   (_ReadPciConfigByteEx)   (GetProcAddress(hModule, "ReadPciConfigByteEx"))
    ReadPciConfigWordEx =   (_ReadPciConfigWordEx)   (GetProcAddress(hModule, "ReadPciConfigWordEx"))
    ReadPciConfigDwordEx =  (_ReadPciConfigDwordEx)  (GetProcAddress(hModule, "ReadPciConfigDwordEx"))

    WritePciConfigByte =    (_WritePciConfigByte)    (GetProcAddress(hModule, "WritePciConfigByte"))
    WritePciConfigWord =    (_WritePciConfigWord)    (GetProcAddress(hModule, "WritePciConfigWord"))
    WritePciConfigDword =   (_WritePciConfigDword)   (GetProcAddress(hModule, "WritePciConfigDword"))

    WritePciConfigByteEx =  (_WritePciConfigByteEx)  (GetProcAddress(hModule, "WritePciConfigByteEx"))
    WritePciConfigWordEx =  (_WritePciConfigWordEx)  (GetProcAddress(hModule, "WritePciConfigWordEx"))
    WritePciConfigDwordEx = (_WritePciConfigDwordEx) (GetProcAddress(hModule, "WritePciConfigDwordEx"))

    FindPciDeviceById =     (_FindPciDeviceById)     (GetProcAddress(hModule, "FindPciDeviceById"))
    FindPciDeviceByClass =  (_FindPciDeviceByClass)  (GetProcAddress(hModule, "FindPciDeviceByClass"))

    # Memory
    if PHYSICAL_MEMORY_SUPPORT:
        ReadDmiMemory =       (_ReadDmiMemory)       (GetProcAddress(hModule, "ReadDmiMemory"))
        ReadPhysicalMemory =  (_ReadPhysicalMemory)  (GetProcAddress(hModule, "ReadPhysicalMemory"))
        WritePhysicalMemory = (_WritePhysicalMemory) (GetProcAddress(hModule, "WritePhysicalMemory"))
    #-----------------------------------------------------------------------------
    # Check Functions
    #-----------------------------------------------------------------------------
    if not (
            GetDllStatus
        and    GetDllVersion
        and    GetDriverVersion
        and    GetDriverType
        and    InitializeOls
        and    DeinitializeOls
        and    IsCpuid
        and    IsMsr
        and    IsTsc
        and    Hlt
        and    HltTx
        and    HltPx
        and    Rdmsr
        and    RdmsrTx
        and    RdmsrPx
        and    Wrmsr
        and    WrmsrTx
        and    WrmsrPx
        and    Rdpmc
        and    RdpmcTx
        and    RdpmcPx
        and    Cpuid
        and    CpuidTx
        and    CpuidPx
        and    Rdtsc
        and    RdtscTx
        and    RdtscPx
        and    ReadIoPortByte
        and    ReadIoPortWord
        and    ReadIoPortDword
        and    ReadIoPortByteEx
        and    ReadIoPortWordEx
        and    ReadIoPortDwordEx
        and    WriteIoPortByte
        and    WriteIoPortWord
        and    WriteIoPortDword
        and    WriteIoPortByteEx
        and    WriteIoPortWordEx
        and    WriteIoPortDwordEx
        and    SetPciMaxBusIndex
        and    ReadPciConfigByte
        and    ReadPciConfigWord
        and    ReadPciConfigDword
        and    ReadPciConfigByteEx
        and    ReadPciConfigWordEx
        and    ReadPciConfigDwordEx
        and    WritePciConfigByte
        and    WritePciConfigWord
        and    WritePciConfigDword
        and    WritePciConfigByteEx
        and    WritePciConfigWordEx
        and    WritePciConfigDwordEx
        and    FindPciDeviceById
        and    FindPciDeviceByClass
    ):
        return False
    if PHYSICAL_MEMORY_SUPPORT:
        if not (
                ReadDmiMemory
            and    ReadPhysicalMemory
            and    WritePhysicalMemory
        ):
            return False
    return InitializeOls()

def DeinitOpenLibSys(hModule):
    if hModule == None:
        return True
    DeinitializeOls()
    FreeLibrary(hModule)
    if hModule != None:
        del hModule
    return hModule == None


